<template>
    <table class="layui-hide" id="table-elem-<%= tableId %>" lay-filter="table-elem-<%= tableId %>"></table>
    <script type="text/html" id="table-toolbarDemo-<%= tableId %>">
        <div class="layui-btn-container">
            <% if(toolbar && toolbar instanceof Array){ %>
                <% toolbar.forEach(item => { %>
                    <button class="layui-btn layui-btn-sm" lay-event="<%= item.name %>"><%= item.title %></button>
                <% }); %>
            <% } %>
        </div>
    </script>
</template>
<script name="compile">
    function templateCompile(data) {
        var defaultData = {
            type: "module",
            tableClick:false
        };
        var newData = Object.assign(defaultData, data);
        var callback = {
            success: true,
            result: newData
        };
        function getLastChild(index){
            if(index == 0){
                if(newData["children"]){
                  return getLastChild(index + 1);
                }else{
                    return "children";
                }
            }else if(newData["children" + index]){
               return getLastChild(index + 1);
            }else{
                return "children" + index;
            }
        }
        if (!newData.tableId) {
            return {
                success: false,
                result: "表格缺少参数：tableId"
            };
        }
        if (newData.toolbar && newData.toolbar instanceof Array) {
            for(let item of newData.toolbar){
                if(!item.title){
                    return {
                        success: false,
                        result: "表格参数toolbar数组缺少参数：title"
                    };
                    break;
                } else if (!item.name) {
                    return {
                        success: false,
                        result: "表格参数toolbar数组缺少参数：name"
                    };
                    break;
                }
                if(item.children){
                    let childrenName = getLastChild(0);
                    newData[childrenName] = item.children;
                    item.childrenName = childrenName;
                }
            };
        }
        if (!newData.requestUrl) {
            return {
                success: false,
                result: "表格缺少参数：requestUrl"
            };
        }
        if (newData.cols || newData.data) {
            if (newData.cols) {
                if(newData.cols instanceof Array){
                    let colsButList = [];
                    for (let item of newData.cols) {
                        if (item instanceof Array) {
                            for (let childItem of item) {
                                if (childItem.templet) {
                                } else if (!childItem.type || childItem.type && childItem.type == "normal" || childItem.templet) {
                                    if (!childItem.field) {
                                        return {
                                            success: false,
                                            result: "表格参数cols数组缺少参数：field"
                                        };
                                        break;
                                    } else if (!childItem.title) {
                                        return {
                                            success: false,
                                            result: "表格参数cols数组缺少参数：title"
                                        };
                                        break;
                                    }
                                }
                                if (childItem.templetBut && childItem.templetBut instanceof Array) {
                                    let templetButState = true;
                                    for (let tempItem of childItem.templetBut) {
                                        if (!tempItem.layEvent) {
                                            return {
                                                success: false,
                                                result: "表格参数cols数组缺少参数：field"
                                            };
                                            templetButState = false;
                                            break;
                                        }
                                        if (tempItem.children) {
                                            let childrenName = getLastChild(0);
                                            newData[childrenName] = tempItem.children;
                                            colsButList.push({
                                                layEvent: tempItem.layEvent,
                                                templet: tempItem.templet || "",
                                                childrenName: childrenName
                                            });
                                        }
                                    }
                                    if (!templetButState) {
                                        break;
                                    }
                                }
                            };
                        } else {
                            return {
                                success: false,
                                result: "表格参数'cols'必须为二维数组"
                            };
                            break;
                        }
                    };
                    callback.result.colsButList = colsButList;
                }else{
                    return {
                        success: false,
                        result: "表格参数'cols'必须为数组"
                    };
                }
            }else if(newData.data){
                if (!newData.data instanceof Array) {
                    return {
                        success: false,
                        result: "表格参数'data'必须为数组"
                    };
                }
            }else{
                return {
                    success: false,
                    result: "表格缺少参数：cols或data"
                };
            }
        }
        
        if (newData.tableClick && newData.tableClick instanceof Object) {
            if(newData.tableClick.children){
                let childrenName = getLastChild(0);
                newData[childrenName] = newData.tableClick.children;
                newData.tableClick.childrenName = childrenName;
            }else if(newData.tableClick.templet){
                
            }else{
                return {
                    success: false,
                    result: "表格参数'tableClick'里必须有参数templet和children其中一种"
                };
            }
        }
        if (newData.where && !newData.where instanceof Array) {
            return {
                success: false,
                result: "表格参数'where'必须为数组"
            };
        }
        let tableData = {
            elem: "#table-elem-"+newData.tableId,
            url: newData.requestUrl,
            cols: newData.cols,
            id: "#table-id-" + newData.tableId
        };
        if(newData.toolbar){
            tableData.toolbar = "#table-toolbarDemo-"+ newData.tableId;
        }
        if (newData.defaultToolbar) {
            tableData.defaultToolbar = newData.defaultToolbar;
        }
        if (newData.title) {
            tableData.title = newData.title;
        }
        if (newData.width) {
            tableData.width = newData.width;
        }
        if (newData.height) {
            tableData.height = newData.height;
        }
        if (newData.cellMinWidth) {
            tableData.cellMinWidth = newData.cellMinWidth;
        }
        if (newData.done) {
            tableData.done = newData.done;
        }
        if (newData.totalRow) {
            tableData.totalRow = newData.totalRow;
        }
        if (newData.page) {
            tableData.page = newData.page;
        }
        if (newData.limit) {
            tableData.limit = newData.limit;
        }
        if (newData.limits) {
            tableData.limits = newData.limits;
        }
        if (newData.loading) {
            tableData.loading = newData.loading;
        }
        if (newData.text) {
            tableData.text = newData.text;
        }
        if (newData.autoSort) {
            tableData.autoSort = newData.autoSort;
        }
        if (newData.initSort) {
            tableData.initSort = newData.initSort;
        }
        if (newData.skin) {
            tableData.skin = newData.skin;
        }
        
        if (newData.method) {
            tableData.method = newData.method;
        }
        if (newData.contentType) {
            tableData.contentType = newData.contentType;
        }
        if (newData.headers) {
            tableData.headers = newData.headers;
        }
        if (newData.request) {
            tableData.request = newData.request;
        }
        if (newData.parseData) {
            tableData.parseData = newData.parseData;
        }
        let tableDataSrt = JSON.stringify(tableData);
        tableDataSrt = tableDataSrt.replace(/}$/, ",'where':whereData}");
        let whereList = [];
        if (newData.where) {
            Object.keys(newData.where).forEach(key => {
                whereList.push({
                    key: key,
                    value: newData.where[key]
                });
            });
        }
        callback.result.whereList = whereList;
        callback.result.tableData = tableDataSrt;
        return callback;
    }
</script>
<script name="templateScript">
    loadTable();
    function loadTable(formData){
        var whereData = {
            <% whereList.forEach(item => { %>
                <%= item.key %>:<%= item.value %>
            <% }); %>
        };
        if(formData){
            for(var key in formData){
                whereData[key] = formData[key];
            }
        }
        table.render(<%- tableData %>);
    }
    <% if(toolbar && toolbar instanceof Array){ %>
    table.on('toolbar(<%= tableData.toolbar %>)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            <% toolbar.forEach(item => { %>
                case '<%= item.name %>':
                var data = checkStatus.data;
                <% if (item.templet) { %>
                    <%- item.templet %>
                <% } %>
                <% if (item.childrenName) { %>
                    //&&script<%= item.childrenName %>&&
                <% } %>
                break;
            <% }); %>
        };
    });
    <% } %>

    <% if (colsButList instanceof Array) { %>
    //监听行工具事件
    table.on('tool(<%= tableData.toolbar %>)', function (obj) {
        var data = obj.data;
        <% colsButList.forEach((item,index) => { %>
            <% if (index !== 0) { %>
                else
            <% } %>
            if (obj.event === '<%= item.layEvent %>'){
                <% if (item.templet) { %>
                    <%- item.templet %>
                <% } %>
                <% if (item.childrenName) { %>
                    //&&script<%= item.childrenName %>&&
                <% } %>
            } 
        <% }); %>
    });
    <% } %>
    <% if (tableClick && tableClick instanceof Object ) { %>
    //监听行单击事件（单击事件为：rowDouble）
        table.on('row(<%= tableData.toolbar %>)', function (obj) {
            var data = obj.data;
            <% if (tableClick.templet) { %>
                <%- tableClick.templet %>
            <% } %>
            <% if (tableClick.childrenName) { %>
                //&&script<%= tableClick.childrenName %>&&
            <% } %>
        });
    <% } %>
</script>